// JavaScript Document

////////////////////////////////////////////////////////////////////////////
var puploadsrc = BASE_URL+"js/swf/puploadcs.swf";
var puploadurl = BASE_URL+"upload/ds/";
var puploadtype = 1;
var puploadftype = "images";//image,video,graphics,images
var puploadparams = [];
var puploadc = 0;
var puploadid = "";
//
var puploaditemhandler = "puploaddelete";
var puploaddeletehandler = function(){};
var puploadselecthandler = function(){};
var puploadstarthandler = function(){};
var puploadcompletehandler = function(){};
//
var puploadpss = [];
//
function puploadinit(ps,target,pid,pft){
	if(pid){
		puploadid = pid;
	}
	if(pft){
		puploadftype = pft;
	}
	//if(puploadid){
		puploadpss[puploadid] = ps;
	//}
	puploadparams = puploadps(ps);
	
	var content = '';
	content += '<div id="PUD'+puploadid+'" class="pupload">';
	content += '<div id="PUPF'+puploadid+'">';
	content += puploadf();
	content += '</div>';
	content += '<div id="PUPP'+puploadid+'"></div>';
	content += '<div id="PUPH'+puploadid+'">';
	content += puploadh();
	content += '</div>';
	content += '</div>';
	if( target && $E(target) ) {
		$E(target).innerHTML = content;
	} else {
		document.write(content);
	}
	
	var puploadobj = puploadfg();
	if(puploadobj){
		//puploadobj.pup_param(puploadid);
	}
}
//
function puploadps(ps){
	if(ps == "pupfile"){
		ps = 'name:pupfile;width:1;height:1;type:thumb;';
	} else if(ps == "graphics"){
		ps = 'name:pupfile;width:1;height:1;type:graphics;';
	} else if(ps == "image"){
		ps = 'name:image;width:1;height:1;type:thumb;';
	} else if(ps == "images"){
		ps = 'name:images;width:1;height:1;type:thumb;multi:1;';
	} else if(ps == "item"){
		ps = 'name:image;width:1;height:1;type:thumb;';
	} else if(ps == "items"){
		ps = 'name:images;width:1;height:1;type:thumb;multi:1;';
	} else {
		ps = 'name:'+ps+';width:1;height:1;type:thumb;';
	}
	var params = [];
	if(ps.indexOf(";") > -1){
		var pss = ps.split(";");
		var pssa = [];
		for(var i=0; i<pss.length; i++){
			pssa = pss[i].split(":");
			if(pssa[0].length > 0){
				params[pssa[0]] = pssa[1];
			}
		}
	}
	params['field_name'] = 'field_'+params['name']+puploadid;
	params['field_width'] = 'field_'+params['name']+'_width'+puploadid;
	params['field_height'] = 'field_'+params['name']+'_height'+puploadid;
	params['field_filename'] = 'field_'+params['name']+'_filename'+puploadid;
	params['field_filesize'] = 'field_'+params['name']+'_filesize'+puploadid;
	params['field_param'] = 'field_'+params['name']+'_param'+puploadid;
	params['param'] = ((puploadparams['param'] == "") ? params['name']+puploadid : puploadparams['param']);
	
	return params;
}
//
function puploadpstype(type){
	puploadparams['type'] = type;
}
//
function puploadpsparam(param){
	puploadparams['param'] = param;
}
//
function puploadpsid(p){
	if(p){
		puploadid = p;
		puploadparams = puploadps(puploadpss[p]);
	}
}
//
function puploadf(){
	var s = getCookie("PPSIDA");
	var content = '';
	content += '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="PUpload'+puploadid+'" width="300" height="40" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">';
	content += '<param name="movie" value="'+puploadsrc+'" />';
	content += '<param name="flashvars" value="upurl='+puploadurl+s+'?t='+puploadparams['type']+'/'+puploadparams['param']+'&upparam='+puploadid+'&uptype='+puploadtype+'&upftype='+puploadftype+'" />';
	content += '<param name="quality" value="high" />';
	content += '<param name="bgcolor" value="#FFFFFF" />';
	content += '<param name="allowScriptAccess" value="sameDomain" />';
	content += '<embed src="'+puploadsrc+'" flashvars="upurl='+puploadurl+s+'?t='+puploadparams['type']+'/'+puploadparams['param']+'&upparam='+puploadid+'&uptype='+puploadtype+'&upftype='+puploadftype+'" quality="high" bgcolor="#FFFFFF" width="300" height="40" name="PUpload'+puploadid+'" align="middle" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer"></embed>';
	content += '</object>';
	
	return content;
}
//
function puploadfh(){
	$E("PUPF"+puploadid).innerHTML = "";	
}
//
function puploadfg(){
	var fn = "PUpload"+puploadid;
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[fn];
	} else {
		return document[fn];
	}
}
//
function puploadh(){
	var content = '';
	var mf = '';
	if(puploadparams['multi'] == 1){
		mf = '[]';
	}
	content += '<input type="hidden" name="'+puploadparams['field_name']+mf+'" id="'+puploadparams['field_name']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_width']+mf+'" id="'+puploadparams['field_width']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_height']+mf+'" id="'+puploadparams['field_height']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_filename']+mf+'" id="'+puploadparams['field_filename']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_filesize']+mf+'" id="'+puploadparams['field_filesize']+'_'+puploadc+'">';
	content += '<input type="hidden" name="'+puploadparams['field_param']+mf+'" id="'+puploadparams['field_param']+'_'+puploadc+'">';
	
	return content;
}
//
function puploadreinit(){
	$E("PUPF"+puploadid).innerHTML = puploadf();
	if(puploadparams['multi'] == 1){
		puploadc++;
		$E("PUPH"+puploadid).innerHTML += puploadh();
	}
}
//
function puploadgc(c){
	var gc = [];
	gc.push($E(puploadparams['field_name']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_width']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_height']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_filename']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_filesize']+'_'+puploadc).value);
	gc.push($E(puploadparams['field_param']+'_'+puploadc).value);
	
	return gc;
}
//
function puploaddelete(obj,c){
	if(confirm("Delete?")){
		dremove(puploadparams['field_name']+'_'+c);
		dremove(puploadparams['field_width']+'_'+c);
		dremove(puploadparams['field_height']+'_'+c);
		dremove(puploadparams['field_filename']+'_'+c);
		dremove(puploadparams['field_filesize']+'_'+c);
		dremove(puploadparams['field_param']+'_'+c);
		dremove('pup_img_'+c);
		//
		if(puploaddeletehandler){
			puploaddeletehandler(c);
		}
	}
}
//
function puploadsizeformat(s){
	if( s > 1024 * 1024 ){
		s = Math.ceil(s*100/(1024*1024)) / 100 + " MB";
	} else if( uf[4] > 1024 ){
		s = Math.ceil(s*100/(1024)) / 100 + " KB";
	} else {
		s = s + " B";
	}
	return s;
}
////////////////////////////////////////////////////////////////////////////
//img
function puploadcompletehandler_selected(obj){
	$E("maincontent").style.backgroundImage = "url("+obj.src+")";
}
//submit
function puploadcompletehandler_submit(){
	
}
//close
function puploadcompletehandler_c(){
	
}
//get current
function puploadcompletehandler_gc(){
	return puploadgc(puploadc);
}
//
function puploadcompletehandler_ds(t){
	puploadfh();
	
	var gc = puploadgc(puploadc);
	$E(t).innerHTML = gc[3];
}
//
function puploadcompletehandler_image(r){
	var rs = r.split(":");
	var c = '<a class="puppd"><img src="'+BASE_URL+rs[0]+rs[1]+'" id="pup_img_'+puploadc+'" onclick="'+puploaditemhandler+'(this,'+puploadc+')"></a>';
	if(puploadparams['multi'] == 1){
		$E("PUPP"+puploadid).innerHTML += c;
	} else {
		$E("PUPP"+puploadid).innerHTML = c;
	}
	
	//
	$E(puploadparams['field_name']+'_'+puploadc).value = rs[1];
	$E(puploadparams['field_width']+'_'+puploadc).value = rs[2];
	$E(puploadparams['field_height']+'_'+puploadc).value = rs[3];
	$E(puploadparams['field_param']+'_'+puploadc).value = rs[4];
}
//
function puploadcompletehandler_file(r){
	$E(puploadparams['field_name']+'_'+puploadc).value = r;
}
////////////////////////////////////////////////////////////////////////////
//ps[0]		filename
//ps[1]		filesize
function puploadselect(ps,p){
	if(p){
		puploadid = p;
		puploadparams = puploadps(puploadpss[p]);
	}
	$E(puploadparams['field_filename']+'_'+puploadc).value = ps[0];
	$E(puploadparams['field_filesize']+'_'+puploadc).value = ps[1];
	//
	if(puploadselecthandler){
		puploadselecthandler();
	}
}
//start
function puploadstart(p){
	if(p){
		puploadid = p;
		puploadparams = puploadps(puploadpss[p]);
	}
	if(puploadstarthandler){
		puploadstarthandler();
	}
}
//complete
function puploadcomplete(r,p){
	//if(p){
		puploadid = p;
		puploadparams = puploadps(puploadpss[p]);
	//}
	r = r.toString();
	if(r.indexOf(":") > -1){
		puploadcompletehandler_image(r);
	} else {
		puploadcompletehandler_file(r);
	}
	//
	if(puploadcompletehandler){
		puploadcompletehandler();
	}
}
//
//pup_param
//pup_select
//pup_upload
//pup_status
////////////////////////////////////////////////////////////////////////////